﻿using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using TruyenCuoiViet.ViewModels;

namespace TruyenCuoiViet.Source.Helper
{
    public class NewsHelper
    {
        public static async Task<ObservableCollection<ItemViewModel>> GetItems(string url, int page)
        {
            ObservableCollection<ItemViewModel> result = new ObservableCollection<ItemViewModel>();
            try
            {
                HtmlDocument d = await CrawlerHelper.GetDocument(url, page);
                HtmlNode n1 = d.DocumentNode.SelectSingleNode("//div[@class='product-list']");
                HtmlNodeCollection ncLi = n1.SelectNodes("ul/li");
                int idNews = 1;
                foreach (HtmlNode node in ncLi)
                {
                    try
                    {
                        string title =HttpUtility.HtmlDecode( node.SelectSingleNode("a").SelectSingleNode("div[1]").InnerText);
                        string detailUrl = MainViewModel.HomeAdress + node.SelectSingleNode("a").Attributes["href"].Value;
                        ItemViewModel n = new ItemViewModel(idNews, title, detailUrl,"");
                        result.Add(n);
                        idNews++;
                    }
                    catch { }
                }
            }
            catch { }

            return result;
        }

        internal async static Task<string> getContent(ItemViewModel nv)
        {
            try
            {
                if (string.IsNullOrEmpty(nv.Content))
                {
                    HtmlDocument d = await CrawlerHelper.GetDocument(nv.DetailUrl);
                    HtmlNode node = d.DocumentNode.SelectSingleNode("//div[@class='full-description']");
                    nv.Content = "<html><head><title>hay</title><meta name='viewport' content='width=device-width, initial-scale=1.0'>"+
                        "<style type='text/css'>p{font: 'Trebuchet MS';font-size: 32px;}</style></head><body>" + node.InnerHtml + "</body><html>";
                    node.InnerHtml = nv.Content;
                    d.LoadHtml(nv.Content);
                    RemoveNode(d.DocumentNode.SelectSingleNode("//script"));
                    nv.Content = d.DocumentNode.InnerHtml;
                }
            }
            catch { }
            return nv.Content;
        }
        private static void RemoveNode(HtmlNode node)
        {
            if (node != null)
                node.Remove();
        }

        //internal async static Task<string> getNewsContentVideo(NewsViewModel nv)
        //{
        //    try
        //    {
        //        if (string.IsNullOrEmpty(nv.NewsContent))
        //        {
        //            HtmlDocument d = await CrawlerHelper.GetDocument(nv.DetailUrl);
        //            HtmlNodeCollection nodes = d.DocumentNode.SelectNodes("//div[@id='admwrapper']/div");
        //            //RemoveNode(nodes[0]);
        //            //RemoveNode(nodes[1]);
        //            //RemoveNode(nodes[3]);
        //            nv.NewsContent = d.DocumentNode.InnerHtml;
        //        }
        //    }
        //    catch { }
        //    return nv.NewsContent;
        //}
    }
}
